/**
 * 数据源类型定义
 */

export enum DataSourceType {
  PROMETHEUS = 'prometheus',
  GRAFANA = 'grafana',
  ZABBIX = 'zabbix',
  NAGIOS = 'nagios',
  DATADOG = 'datadog',
  CLOUDWATCH = 'cloudwatch',
  ELASTICSEARCH = 'elasticsearch',
  CUSTOM_WEBHOOK = 'custom_webhook',
  KAFKA = 'kafka',
}

export enum DataSourceStatus {
  ACTIVE = 'active',
  INACTIVE = 'inactive',
  ERROR = 'error',
  TESTING = 'testing',
}

export interface DataSourceConfig {
  id: string;
  name: string;
  type: DataSourceType;
  description?: string;
  status: DataSourceStatus;
  enabled: boolean;
  priority: number; // 数据源优先级，数字越小优先级越高

  // 连接配置
  connection: {
    url: string;
    apiKey?: string;
    username?: string;
    password?: string;
    headers?: Record<string, string>;
    timeout?: number;
    retryAttempts?: number;
    retryDelay?: number;
  };

  // 数据映射配置
  mapping: {
    alertNameField: string;
    severityField: string;
    statusField: string;
    timestampField: string;
    descriptionField: string;
    labelsField?: string;
    annotationsField?: string;
    customFields?: Record<string, string>;
  };

  // 字段值映射
  valueMapping: {
    severity?: {
      [key: string]: 'critical' | 'high' | 'medium' | 'low';
    };
    status?: {
      [key: string]: 'firing' | 'resolved' | 'acknowledged';
    };
  };

  // 过滤规则
  filters?: {
    includeRules?: FilterRule[];
    excludeRules?: FilterRule[];
  };

  // 数据转换
  transform?: {
    enabled: boolean;
    script?: string; // JavaScript 脚本用于自定义转换
  };

  // 健康检查
  healthCheck?: {
    enabled: boolean;
    interval: number; // 秒
    endpoint?: string;
  };

  // 元数据
  metadata?: {
    createdBy: string;
    lastSyncAt?: Date;
    lastHealthCheck?: Date;
    totalAlertsReceived?: number;
    errorCount?: number;
  };
}

export interface FilterRule {
  field: string;
  operator: 'equals' | 'contains' | 'startsWith' | 'endsWith' | 'regex' | 'gt' | 'lt' | 'gte' | 'lte';
  value: string | number | boolean;
}

// 标准化的告警接口
export interface StandardAlert {
  id: string;
  sourceId: string; // 数据源 ID
  sourceType: DataSourceType;
  alertname: string;
  severity: 'critical' | 'high' | 'medium' | 'low';
  status: 'firing' | 'resolved' | 'acknowledged';
  instance: string;
  startsAt: Date;
  endsAt?: Date;
  description: string;
  labels: Record<string, string>;
  annotations: Record<string, string>;
  fingerprint: string;
  rawData?: any; // 原始数据
}

// 数据源适配器接口
export interface DataSourceAdapter {
  type: DataSourceType;

  // 验证连接
  testConnection(config: DataSourceConfig): Promise<{
    success: boolean;
    message: string;
    latency?: number;
  }>;

  // 获取告警
  fetchAlerts(config: DataSourceConfig, options?: {
    startTime?: Date;
    endTime?: Date;
    filters?: Record<string, any>;
  }): Promise<StandardAlert[]>;

  // 转换原始数据为标准格式
  transformAlert(rawData: any, config: DataSourceConfig): StandardAlert;

  // 健康检查
  healthCheck(config: DataSourceConfig): Promise<boolean>;
}

// 数据源统计信息
export interface DataSourceStats {
  id: string;
  name: string;
  type: DataSourceType;
  status: DataSourceStatus;
  alertsReceived: number;
  lastSyncAt?: Date;
  averageLatency: number;
  errorRate: number;
  successRate: number;
}

// 数据源测试结果
export interface DataSourceTestResult {
  success: boolean;
  message: string;
  details?: {
    connectionTest: boolean;
    authTest: boolean;
    dataFetchTest: boolean;
    sampleAlerts?: StandardAlert[];
    latency: number;
    errors?: string[];
  };
}
